﻿using System.Collections.Generic;
using System.ComponentModel;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using Lwllm.ArcengineProxy.Common;
using Lwllm.ArcengineProxy.Core.Annotations;
using Lwllm.ArcengineProxy.Core.Proxys;

namespace Lwllm.ArcengineProxy.Core.Models
{
    public class BaseEntity : FeatureProxy
    {
        private static Dictionary<string, string> _addedDescriptors = new Dictionary<string, string>();

        public BaseEntity(IFeature feature, FeatureClassProxy featureClassProxy) : base(feature, featureClassProxy)
        {
            if (!_addedDescriptors.ContainsKey($"{featureClassProxy.FeatureClassProxyId}_{GetType().FullName}"))
            {
                var fidFieldName = featureClassProxy.FeatrueClass.OIDFieldName;
                if (!string.IsNullOrWhiteSpace(fidFieldName))
                {
                    FieldNameAttribute.AddDescriptor(this.GetType(), "Fid", fidFieldName);
                }
                var lengthFieldName = featureClassProxy.FeatrueClass.LengthField.Name;
                if (!string.IsNullOrWhiteSpace(lengthFieldName))
                {
                    FieldNameAttribute.AddDescriptor(this.GetType(), "Length", lengthFieldName);
                }
                var areaFieldName = featureClassProxy.FeatrueClass.AreaField.Name;
                if (!string.IsNullOrWhiteSpace(areaFieldName))
                {
                    FieldNameAttribute.AddDescriptor(this.GetType(), "Area", areaFieldName);
                }
                _addedDescriptors.Add($"{featureClassProxy.FeatureClassProxyId}_{GetType()}", "");
            }
        }

        public int Fid
        {
            get { return Feature.OID; }
        }

        public double Length
        {
            get { return ((IPolyline)Feature.Shape)?.Length ?? 0; }
        }

        public double Area
        {
            get { return ((IArea)Feature.Shape)?.Area ?? 0; }
        }
    }
}